package com.herui.iot.ui.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;


import com.herui.sdyu_lib.adapter.recycle_view.CommonAdapter;
import com.herui.sdyu_lib.adapter.recycle_view.base.ViewHolder;
import com.herui.sdyu_lib.utils.LogUtils;
import com.herui.sport_care.R;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by amanda.shan on 2022/5/22.
 */
public class PopDialog {

    private Context context;
    RecyclerView idRecyclerview;

    private SelectLis selectLis;

    private List<String> mDatas = new ArrayList<>();
    private CommonAdapter<String> mAdapter;
    private String selectItem;

    private PopupWindow popWindow;

    public PopDialog(Context context, View appendView, int itemLayout, List<String> mDatas,
                     String selectItem, SelectLis selectLis) {
        this.mDatas = mDatas;
        this.context = context;
        this.selectItem = selectItem;
        this.selectLis = selectLis;

        View view = LayoutInflater.from(context).inflate(R.layout.layout_popup, null, false);
        idRecyclerview = view.findViewById(R.id.id_recyclerview);
        init(itemLayout);

        //1.构造一个PopupWindow，参数依次是加载的View，宽高
        popWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

//        popWindow.setAnimationStyle(R.anim.anim_pop);  //设置加载动画

        //这些为了点击非PopupWindow区域，PopupWindow会消失的，如果没有下面的
        //代码的话，你会发现，当你把PopupWindow显示出来了，无论你按多少次后退键
        //PopupWindow并不会关闭，而且退不出程序，加上下述代码可以解决这个问题
        popWindow.setTouchable(true);
        popWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });
        popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));    //要为popWindow设置一个背景才有效


        //设置popupWindow显示的位置，参数依次是参照View，x轴的偏移量，y轴的偏移量
        popWindow.showAsDropDown(appendView, 0, 10);

        LogUtils.d("showAsDropDown");
    }

    private void init(int itemLayout) {
        idRecyclerview.setLayoutManager(new LinearLayoutManager(context));

        // 单个布局
        mAdapter = new CommonAdapter<String>(context, itemLayout, mDatas) {
            @Override
            public void convert(ViewHolder holder, String s, final int position) {
                holder.setText(R.id.tv_text, s);
                if (s.equals(selectItem)){
                    holder.setBackgroundColor(R.id.view_item, 0xff3568D1);
                }
            }
        };
        idRecyclerview.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                LogUtils.d("onItemClick:" + mDatas.get(position));
                selectLis.onSelect(mDatas.get(position));
                mAdapter.notifyDataSetChanged();
                popWindow.dismiss();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int
                    position) {
                return false;
            }
        });
    }

    public static interface SelectLis {
        void onSelect(String value);
    }
}
